package jeecg.business.thread.task.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.task.ExecTask;

import org.apache.commons.lang.math.RandomUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.CommonHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.FetchListInfo;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.clean.bean.InfoParam;
/**
 * 
 * <p>
 * 标题: 公用抓取任务
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-6 下午2:56:28
 */
public class CommonExecTask extends ExecTask{
	
	
	protected int current = 0;
	
	protected String cookies;

	public CommonExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}
	
	public void setCookies(){}
	
	public String getFetchHtml() {
		String basehref = site.getZddz();
		String word;
		try {
			word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			return basehref;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			return null;
		}
	}
	
	public String getLongStartTime(){
		Date now = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(now);
		cl.add(Calendar.MINUTE, -5);
		cl.add(Calendar.MONDAY, -1);
		return cl.getTime().getTime()+"";
	}
	
	public String getStartTime(){
		Date now = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(now);
		cl.add(Calendar.MINUTE, -5);
		cl.add(Calendar.MONDAY, -1);
		return cl.getTime().getTime()/1000+"";
	}
	
	public String getEndTime(){
		Date now = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(now);
		cl.add(Calendar.MINUTE, -5);
		return cl.getTime().getTime()/1000+"";
	}
	
	public void run(){
		CollLogEntity log = null;
		log = saveLog();
		try{
			
			System.out.println("exec common task......");
			String basehref = site.getZddz();
			String word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			String paramBaseHref = basehref;
			setCookies();
			do{
				current++;
				if(StringUtil.isNotEmpty(basehref)){
					
					System.out.println(keyword.getKeyword()+"-----"+site.getMc()+"-----第"+current+"页"+"-----"+basehref);
					
					basehref = basehref.replace("&amp;", "&");
					
					FetchListInfo info = CommonHtmlCleanUtil.getListUrl(basehref, site , cookies);
					
					if("param".equals(site.getXyyhqfs())){
						if(!basehref.contains("{page}")) basehref = paramBaseHref + "&"+site.getLbxyy()+"={page}";
						basehref = basehref.replace("{page}", (current+1)+"");
						info.setNextPage(basehref);
					}else{
						basehref = info.getNextPage();
					}
					List<String> urlList = info.getHrefList();
					if(urlList!=null && urlList.size()>0){
						
						HtmlParseResult result = null;
						KeyinfoEntity keyinfo = null;
						for(String url : urlList){
							url = url.replace("&amp;", "&");
							if(keyinfoService.hasHref(url , enter.getId())) continue;
							/**
							 * 单个错误时跳过
							 */
							if(url.contains("sdgdwljt"))
								System.out.println("test");
							try{
								InfoParam infoparam = new InfoParam();
								infoparam.setListInfo(info);
								result = CommonHtmlCleanUtil.getInfo(url, site, keyword.getKeyword() , cookies , infoparam);
								keyinfo = new KeyinfoEntity(result);
								keyinfo.setQyid(enter.getId());
								System.out.println(result.getUrl()+"----"+result.getTitle()+"----"+(result.getBodycontent()!=null?result.getBodycontent().length():0)+"----"+result.getEventDate());
								if(filter(enter, keyinfo , keyword))		//过滤
									keyinfoService.saveKeyinfo(keyinfo);
							}catch(Exception e1){
								e1.printStackTrace();
							}							
						}
						
					}
				}
				if("wx".equals(site.getJclx())){
					Thread.sleep((15+RandomUtils.nextInt(10))*1000);
				}
			}while(current <= maxpage);
//			log = collLogService.get(CollLogEntity.class, log.getId());
		}catch(Exception e){
			e.printStackTrace();
//			log.setSfcg("2");
//			log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}			
	}

}
